package controllers

import (
	"Learn/base"
	"Learn/commit"
	"Learn/model"

	"github.com/kataras/iris"
)

type BaseController struct {
}

func (*BaseController) Get(ctx iris.Context) {
	id, err := ctx.URLParamInt64("id")
	if err != nil {
		ResponseError(ctx, nil, 1000, "参数ID不正确")
		return
	}

	var baseMod model.Base
	err = base.DB.Model(&baseMod).Where("id=?", id).Limit(1).Select()
	if err != nil {
		ResponseError(ctx, err, 1000, "无法获取数据")
		return
	}

	ResponseSuccess(ctx, baseMod, 0, "获取成功")
}

func (*BaseController) Add(ctx iris.Context) {
	var data model.Base
	err := ctx.ReadJSON(&data)

	if err != nil {
		ResponseError(ctx, err, 1000, "请求参数不正确")
		return
	}
	data.ID = commit.IDnode.Generate().Int64()

	_, err = base.DB.Model(&data).Insert()
	if err != nil {
		ResponseError(ctx, err, 2000, "添加数据失败")
		return
	}

	ResponseSuccess(ctx, data, 200, "创建成功")
}
